﻿

//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "IType.h"
#include "IFamily.h"
#include "IInstance.h"
#include "IDocument.h"
#include "UniIdentity.h"
#include "IGenericType.h"
#include "IInstanceType.h"
#include "IElementFilter.h"
#include "ILinkElementProxy.h"

#include "DocumentUtils.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;
using namespace Sample;

class TypeFilter : public IElementFilter
{

public:
    virtual bool PassesFilter(const IElement* pElement) const override
    {
        const IType* pType = quick_cast<IType>(pElement);
        return pType != nullptr;
    }

};

std::vector<gcmp::IType*> Sample::DocumentUtils::GetTypiesByCategory(const IDocument* pDocument, const gcmp::UniIdentity& categoryUid)
{
    OwnerPtr<TypeFilter> opTypeFilter = NEW_AS_OWNER_PTR(TypeFilter);
    std::vector<gcmp::IElement*> pElements = pDocument->GetElementsByCategory(categoryUid, opTypeFilter.get());
    std::vector<IType*> pTypes;
    FOR_EACH(pElement, pElements)
    {
        IType* pType = quick_cast<IType>(pElement);
        if (pType)
            pTypes.push_back(pType);
    }
    return pTypes;
}

std::vector<IElement*> Sample::DocumentUtils::GetElementsByCategoryExceptFamilyAndType(const IDocument* pIDoc, const UniIdentity& categoryUid, bool excludeLinkElementProxy)
{
    const IDocument* pDoc = quick_cast<IDocument>(pIDoc);
    DBG_WARN_AND_RETURN_UNLESS(pDoc, std::vector<IElement*>(), L"无效参数",L"GDMP",L"2023-10-20");
    std::vector<IElement*> pAllElements = pDoc->GetElementsByCategory(categoryUid);
    std::vector<IElement*> pElements;
    FOR_EACH(pElement, pAllElements)
    {
        IGenericType* tempGType = quick_cast<IGenericType>(pElement);
        if (tempGType)
        {
            pElements.push_back(pElement);
            continue;
        }

        IType* pType = quick_cast<IType>(pElement);
        IFamily* pFamily = quick_cast<IFamily>(pElement);
        if (pType || pFamily)
            continue;

        if (excludeLinkElementProxy && quick_cast<ILinkElementProxy>(pElement))
            continue;

        pElements.push_back(pElement);
    }
    return pElements;
}

std::vector<const IInstance*> Sample::DocumentUtils::GetFamilyInstances(gcmp::IDocument * pDoc, const std::wstring& familyName)
{
    std::vector<const gcmp::IInstance*> allInstances;

    IFamily* family = IFamily::GetFamilyByName(pDoc, familyName);
    if (family)
    {
        auto types = family->GetInstanceTypes();
        FOR_EACH(type, types)
        {
            const std::vector<IInstance*>& instances = type->GetAllInstances();
            allInstances.insert(allInstances.end(), instances.begin(), instances.end());
        }
    }

    return allInstances;
}
